<template>
  <div>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="checkNeedDoubleCheck()" label-width="150px">
    <el-form-item label="行政地区代码" prop="code">
      <el-input v-model="dataForm.code" placeholder="行政地区代码"></el-input>
    </el-form-item>
    <el-form-item label="行政地区名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="行政地区名称"></el-input>
    </el-form-item>

    <el-form-item label="行政级别" prop="grade">
      <el-select v-model="dataForm.grade" placeholder="行政级别" style="width:100%">
          <el-option
            v-for="item in dataForm.regionLevelList"
            :key="item.value"
            :label="item.text"
            :value="item.value">
          </el-option>
       </el-select>
    </el-form-item>

    <el-form-item label="归属上级区域代码" prop="upregion">
      <el-input v-model="dataForm.upregion" placeholder="归属上级区域代码"></el-input>
    </el-form-item>

    <el-form-item label="是否开通登记业务" v-if="dataForm.grade == '2'" prop="openflag">

      <el-switch
        v-model="dataForm.openflag"
        active-text="开通"
        inactive-text="未开通">
      </el-switch>

    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="checkNeedDoubleCheck()">确定</el-button>
    </span>
  </el-dialog>

  <double-check v-if="doubleCheckVisible" ref="doubleCheck"  @refreshDataList="dataFormSubmit" ></double-check>

  </div>
</template>

<script>

  import DoubleCheck from './double-check'

  export default {
    data () {
      return {
        visible: false,
        needDoubleCheck: false,
        doubleCheckVisible: false,
        dataForm: {
          seqno: 0,
          openflag: false,
          code: '',
          name: '',
          grade: '',
          upregion: '',
          regionLevelList: []
        },
        dataRule: {
          code: [
            { required: true, message: '行政地区代码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '行政地区名称不能为空', trigger: 'blur' }
          ],
          grade: [
            { required: true, message: '行政级别不能为空', trigger: 'blur' }
          ],
          upregion: [
            { required: true, message: '归属上级区域代码不能为空', trigger: 'blur' }
          ]
        }
      }
    },


    components: {
      DoubleCheck
    },

    methods: {
      init (id) {
        this.dataForm.seqno = id || 0
        this.visible = true

        this.dataForm.openflag = false

        this.$http({
          url: this.$http.adornUrl(`/sys/bdcregion/region/list`),
              method: 'get',
              params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.regionLevelList = data.regionLevelList
              this.needDoubleCheck = data.needDoubleCheck

            }
        })

        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.seqno) {
            this.$http({
              url: this.$http.adornUrl(`/sys/bdcregion/info/${this.dataForm.seqno}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.bdcRegion.code
                this.dataForm.name = data.bdcRegion.name
                this.dataForm.grade = data.bdcRegion.grade
                this.dataForm.upregion = data.bdcRegion.upregion

                this.dataForm.openflag = data.bdcRegion.openflag == '1' ? true : false
              }
            })
          }
        })
      },

      checkNeedDoubleCheck(){
        if (this.needDoubleCheck){
          this.$refs['dataForm'].validate((valid) => {
              if (valid) {
                this.doubleCheckVisible = true;
                this.$nextTick(() => {
                  this.$refs.doubleCheck.init()
                })
              }
          })
        }else{
          this.dataFormSubmit ();
        }
      },

      // 表单提交
      dataFormSubmit (username, passwd, callback) {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/bdcregion/${!this.dataForm.seqno ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'seqno': this.dataForm.seqno || undefined,
                'code': this.dataForm.code,
                'name': this.dataForm.name,
                'grade': this.dataForm.grade,
                'upregion': this.dataForm.upregion,
                'openflag': this.dataForm.openflag ? '1' : '0',
                'doubleCheck': {
                  'username': username,
                  'passwd': passwd
                }
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    if (callback){
                      callback()
                    }
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
